package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 获取当前用户的配送地址信息
     * @return
     */
    @Override
    public List<AddressBook> list() {
        //获取当前登录的信息
        Long userId = BaseContext.getCurrentId();
        List<AddressBook> list = addressBookMapper.list(userId);
        return list;
    }

    /**
     * 新增地址
     * @param addressBook
     */
    @Override
    public void save(AddressBook addressBook) {
        //获取当前登录的信息
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBookMapper.save(addressBook);
    }

    /**
     * 根据id查询地址
     * @param id
     */
    @Override
    public AddressBook getById(Long id) {
        AddressBook addressBook = addressBookMapper.getById(id);
        return addressBook;
    }

    /**
     * 根据id修改地址
     * @param addressBook
     */
    @Override
    public void updateById(AddressBook addressBook) {
        addressBookMapper.updateById(addressBook);
    }

    /**
     * 根据id删除地址
     * @param id
     */
    @Override
    public void delById(Long id) {
        addressBookMapper.delById(id);
    }

    /**
     * 设置默认地址
     * @param id
     */
    @Override
    public void setDefault(Long id) {
        //将默认地址删除
        //获取当前登录的信息
        Long userId = BaseContext.getCurrentId();
        addressBookMapper.UpdateDefault(userId);
        //设置默认地址
        addressBookMapper.setDefault(id);

    }

    /**
     * 查询默认地址
     * @return
     */
    @Override
    public AddressBook getDefault() {
        //获取当前登录的信息
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = addressBookMapper.getDefault(userId);
        return addressBook;
    }

}
